﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Win8Start.UI
{
	public partial class ShortcutPanel : UserControl
	{
		public ShortcutPanel()
		{
			InitializeComponent();
			SetStyle(ControlStyles.Selectable, true);
		}

		List<string> Ignorefiles = new List<string> { "desktop.ini" };

		#region Private Variables

		private int _rowHeight = 16;
		private int _rowSpacing = 4;
		private SortedList<string, MenuItemControl> Items = new SortedList<string, MenuItemControl>();
		private int NextY;
		#endregion

		#region Events
		public class MenuItemEventArgs : EventArgs
		{
			public MenuItemControl MenuItem;
		}
		public delegate void ItemSelectedEvent(object sender, MenuItemEventArgs e);
		public event ItemSelectedEvent ItemSelected;
		public void onItemSelected(MenuItemEventArgs e)
		{
			if (ItemSelected != null)
				ItemSelected(this, e);
		}
		#endregion

		#region Properties

		/// <summary>
		/// Height of objects added to the panel
		/// </summary>
		public int RowHeight
		{
			get { return _rowHeight; }
			set { _rowHeight = value; }
		}

		/// <summary>
		/// Space in between objects on the menu
		/// </summary>
		public int RowSpacing
		{
			get { return _rowSpacing; }
			set { _rowSpacing = value; }
		}

		#endregion

		public void Clear()
		{
			Items.Clear();
			ShowList();
			Application.DoEvents();
		}

		virtual public void LoadFolder(string FolderName)
		{
			if (FolderName == null || FolderName == "")
				return;

			if (Directory.Exists(FolderName))
			{
				string[] folders = System.IO.Directory.GetDirectories(FolderName);
				foreach (string folder in folders)
				{
					AddItem(folder);
				}

				string[] files = System.IO.Directory.GetFiles(FolderName);
				foreach (string file in files)
				{
					string fn = Path.GetFileName(file).ToLower();
					if (!Ignorefiles.Contains(fn))
						AddItem(file);
				}
			}
			else
			{
				MessageBox.Show("Folder not found: " + FolderName);
			}
		}

		/// <summary>
		/// Reload the controls on this panel. Call this AFTER inherited classes have 
		/// refreshed the underlying data.
		/// </summary>
		virtual public void ReloadAll()
		{
			ShowList();
		}

		protected void ShowList()
		{
			bool v = this.Visible;
			this.Visible = true;

			Controls.Clear();
			NextY = 0;
			this.AutoScroll = false;
			AddControls(Items);
			this.AutoScroll = true;

			this.Visible = v;
		}

		protected virtual void AddControls(SortedList<string, MenuItemControl> items)
		{
			foreach (string i in items.Keys)
			{
				MenuItemControl mi = items[i];
				Controls.Add(mi);
				mi.Left = 0;
				mi.Width = this.ClientRectangle.Width;
				mi.Top = NextY;
				mi.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
				NextY += RowHeight + RowSpacing;
				mi.MouseClick += new MouseEventHandler(mi_MouseClick);
				mi.MouseEnter += new EventHandler(ShortcutPanel_MouseEnter);
			}
		}

		protected virtual void mi_MouseClick(object sender, MouseEventArgs e)
		{
			MenuItemControl mi = sender as MenuItemControl;

			if (mi != null)
			{
				MenuItemEventArgs ea = new MenuItemEventArgs();
				ea.MenuItem = mi;
				onItemSelected(ea);
			}
		}

		virtual public MenuItemControl AddItem(string ItemPath)
		{
			MenuItemControl mi = new MenuItemControl();
			mi.Shortcut = new Shortcut(ItemPath);

			string name = mi.Shortcut.Name;
			if (Items.ContainsKey(name))
				Items[name] = mi;
			else
				Items.Add(name, mi);

			return mi;
		}

		private void ShortcutPanel_MouseEnter(object sender, EventArgs e)
		{
			if (!this.ContainsFocus)
			{
				this.Focus();
			}
		}

		virtual public MenuItemControl Add(string Key, string Command, string Caption, Icon Icon)
		{
			Shortcut s = new Shortcut();
			s.FileName = Command;
			s.Name = Caption;
			s.Icon = Icon;

			MenuItemControl mi = new MenuItemControl();
			mi.Shortcut = s;
			Items.Add(Key, mi);
			return mi;
		}

		virtual public int Count
		{
			get { return Items.Count; }
		}

		/// <summary>
		/// Total height of all the items in this list
		/// </summary>
		virtual public int TotalHeight
		{
			get
			{
				int h = (RowHeight + RowSpacing) * Count + RowSpacing;
				if (this.MaximumSize.Height > h)
					return this.MaximumSize.Height;
				if (h < this.MinimumSize.Height)
					return this.MinimumSize.Height;

				int adjust = this.Height - this.ClientRectangle.Height + 2;
				return h + adjust;
			}
		}

	}
}
